package org.weishe.join;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.StringUtils;
import org.weishe.fof.FOF;
import org.weishe.fof.FOFMain;
import org.weishe.fof.FOFMapper;
import org.weishe.fof.FOFReducer;

public class Join {
	public static class JoinMaper extends Mapper<LongWritable, Text, Text, Text> {
		private String flag;
		private Text outKey = new Text();
		private Text outValue = new Text();

		@Override
		protected void setup(Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			FileSplit split = (FileSplit) context.getInputSplit();
			flag = split.getPath().getName();// 判断读的数据集

			System.out.println(flag + "**********************");
		}

		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			String str[] = StringUtils.split(value.toString(), '\t');
			if (flag.equals("address.txt")) {
				outKey.set(str[0]);
				outValue.set("A:" + str[1]);
			} else {
				outKey.set(str[1]);
				outValue.set(str[0]);
			}
			context.write(outKey, outValue);
		}

	}

	public static class JoinReducer extends Reducer<Text, Text, Text, NullWritable> {
		private String address = "";
		private Text outKey = new Text();
		private NullWritable nw = NullWritable.get();

		@Override
		protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, NullWritable>.Context context)
				throws IOException, InterruptedException {
			ArrayList<String> al = new ArrayList<String>();
			for (Text v : values) {
				if (v.toString().startsWith("A:")) {
					address = (StringUtils.split(v.toString(), ':'))[1];
					break;
				} else {
					al.add(v.toString());
				}
			}
			for (String v : al) {
				if (!v.startsWith("A:")) {
					outKey.set(v + "\t" + address);
					context.write(outKey, nw);
				}
			}

		}

	}

	public static void main(String[] args) throws Exception {
		String inputPath = "hdfs://192.168.15.10:9000/usr/input/join";
		String outputPath = "hdfs://192.168.15.10:9000/output/join" + (new Date()).getTime();

		// 1、配置文件
		Configuration conf = new Configuration();

		Job job = Job.getInstance(conf);
		job.setJarByClass(Join.class);
		// 2、设置Job

		job.setJobName("  join");

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		//
		// job.setInputFormatClass(KeyValueTextInputFormat.class);
		// 如果是mapper 和reduce的输出是一样的name就采用这个设置
		// job.setOutputKeyClass(FOF.class);
		// job.setOutputValueClass(IntWritable.class);

		job.setMapperClass(JoinMaper.class);
		job.setReducerClass(JoinReducer.class);

		// 设置reducer的数量
		// job.setNumReduceTasks(3);

		FileInputFormat.setInputPaths(job, new Path(inputPath));
		Path op = new Path(outputPath);
		// if (fs.exists(op)) {
		// fs.delete(op, true);
		// }
		FileOutputFormat.setOutputPath(job, op);

		boolean f = job.waitForCompletion(true);
		if (f) {
			System.out.println("job 成功执行");
		}
	}

}
